import util from '@/utils/util.js';




export default class Http {
	constructor(url) {
		this.url = url;
		this.header = {
			'Content-Type': 'application/x-www-form-urlencoded'
		}
	}
	request(url, param = {
		data: {},
	}, type = 'request', header = {}) {
		return new Promise((resolve, rejected) => {
			let params = {
				url: this.url + url,
				header: {
					...this.header,
					...header
				},
				method: 'POST',
				...param,
			};

			
			
			let token = uni.getStorageSync('token');
			// 不需要登录也可以访问的接口
			let urlList = [
				// '/jxtc/enterprise/queryStoresByKeyword',
				 "/employee/indexPage/loginWx",
				'/employee/indexPage/loginUserPassword',
			];
			token && (params.header['X-Access-Token'] = token);
			console.log(urlList,url);
			if (urlList.indexOf(url) == -1) {
				if (!token) {
					uni.showToast({
						title: '登录已过期，请重新登录!',
						icon: 'none'
					})
					let routes = getCurrentPages();
					let curRoute = routes[routes.length - 1].route
					if (curRoute != 'pages/index/index') {
						setTimeout(() => {
							uni.reLaunch({
								url: "/pages/index/index"
							})
						}, 1500)
					}
					return;
				}else{
					let tokenExp = uni.getStorageSync('tokenExp');
					if((tokenExp * 1000) <=  Date.parse(new Date())){
						// console.log("token 过期时间到了",tokenExp);
						uni.removeStorageSync('token');
					}
				}
			}
			if(params.type == 'urlencoded'){
				params.header['Content-Type'] = 'application/x-www-form-urlencoded'
			}else if (params.method.toLowerCase() == "post" || params.type == 'json' || params.method.toLowerCase() == 'put') {
				params.header['Content-Type'] = 'application/json'
			}
			
			// if(params.method.toLowerCase() == 'put'){
			// 	params.header['Content-Type'] = 'multipart/form-data'
			// }
			// delete 、put兼容 ，参数不能放在body. 和接口地址拼接(类似get)
			// if(params.method.toLowerCase() == "delete" || params.method.toLowerCase() == "put"){
			// 	params.header['Content-Type'] = 'application/x-www-form-urlencoded'
			// 	let datas = [];
			// 	for(var key in params.data){
			// 		datas.push(`${key}=${params.data[key]}`);
			// 	}
			// 	params.url =  encodeURI((`${params.url}?${datas.join('&')}`).replace(/\+/g,"%2B"));
			// 	console.log(params.url,'delete 、put兼容');
			// 	delete params.data;
			// }
			

			if (type != 'request') {
				if (!params.data[type]) {
					return;
				}
				let filePath = JSON.parse(JSON.stringify(params.data[type]));
				delete params.data[type];
				let data = JSON.parse(JSON.stringify(params.data))
				delete params.data;


				uni.uploadFile({
					filePath,
					name: type,
					formData: data,
					...params,
					success: (res) => {
						let data = JSON.parse(res.data);
						resolve(data)
						// this.clearInfo(data)
					},
					fail(err) {
						resolve(err);
					}
				})
			} else {
				uni.request({
					...params,
					success: (res) => {
						resolve(res.data)
						// this.clearInfo(res.data)
					},
					fail(err) {
						resolve(err);
					}
				})
			}

		})
	}
	clearInfo(data) {
		let {
			code,
			msg
		} = data;
		if (code == 2) {
			uni.removeStorageSync('token');
			// setTimeout(()=>{
			// 	util.goToUrl('/pages/personal/index','switchTab')
			// },1000)
		}

	}
}